package com.interview.domain;

import lombok.Getter;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class Order {
    List<OrderItem> orderList;
    @Getter
    BigDecimal couponVal;
    BigDecimal originalTotal;
    BigDecimal finalTotal;

    public Order() {
        orderList = new ArrayList<>();
    }

    public BigDecimal getOriginalTotal() {
        double sum = orderList.stream().mapToDouble(p -> p.getTotal().doubleValue()).sum();
        this.originalTotal = new BigDecimal(sum);
        return originalTotal.setScale(2, RoundingMode.UP);
    }

    public BigDecimal getFinalTotal() {
        this.originalTotal = getOriginalTotal();
        this.finalTotal = couponVal!=null? originalTotal.subtract(couponVal):originalTotal;
        return finalTotal.setScale(2, RoundingMode.UP);
    }

    public void addOrderItem(OrderItem item){
        orderList.add(item);
    }

    public void setCouponVal(BigDecimal couponVal) {
        this.couponVal = couponVal;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (OrderItem orderItem : orderList) {
            sb.append(orderItem).append("\n");
        }
        return  "订单列表:\n" + sb +
                (couponVal!=null?("优惠:" + couponVal+"\n"):"" )+
                "原价:" + getOriginalTotal() + "\n"+
                "现价:" + getFinalTotal();
    }


}
